package com.jml.proxy;

import com.jml.annotation.Select;
import com.jml.entity.UserEntity;
import com.jml.session.SqlSession;
import com.jml.statement.MappedStatement;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;


public class MapperProxy<T> implements InvocationHandler {

    private SqlSession sqlSession;

    public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface) {
        this.sqlSession = sqlSession;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //关联对应的MappedStatement
        //是否有对应的注解
        Annotation annotation = method.getAnnotation(Select.class);
        if(annotation == null){
            throw new Exception("Mapped没有配置相关注解");
        }
        MappedStatement mappedStatement = new MappedStatement(method,args);
        return sqlSession.selectOne(mappedStatement);
    }
}
